﻿Public Class ActivitHumSit
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim Niveau As Integer = 1
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
    Dim idAct As String = ""
    Public databaseName As String
    Private Sub ActivitHumSit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBoxCodeCatg.Visible = False
        Me.ComboBoxCodeTyp.Visible = False
        Me.ComboBoxCodeAct.Visible = False


        Entrer = ""
        DataGridViewTypol.MultiSelect = False
        DataGridViewTypol.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewTypol.ReadOnly = True


        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'EspaceTyp'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 7
        End While
        db.Disconnecter()

        LibelleNiv = New String(NbrNiv - 1) {}
        CodeNiv = New String(NbrNiv - 1) {}
        NomNiv = New String(NbrNiv - 1) {}
        IdNiv = New String(NbrNiv - 1) {}
        Nomtable = New String(NbrNiv + 7) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='EspaceTyp' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'EspaceTyp'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()
        majCateg()
        majTyp()
        majAut()
        majCatgEsp()
        miseajour()
    End Sub


    Private Sub majCateg()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[ActvCateg]"
            db.Connecter(databaseName)
            Me.ComboBoxCodeCatg.Items.Clear()
            Me.ComboBoxNomCatg.Items.Clear()
            Me.ComboBoxCodeCatg.Items.Add("")
            Me.ComboBoxNomCatg.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxCodeCatg.Items.Add(db.rdr(0))
                Me.ComboBoxNomCatg.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxCodeCatg.SelectedIndex = 0
            Me.ComboBoxNomCatg.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub majTyp()
        Try
            Entrer = ""
            If Me.ComboBoxNomCatg.Text = "Tous" Then
                db.myReq = "Select * FROM  [dbo].[ActivHumTyp]"
            Else
                db.myReq = "Select * FROM  [dbo].[ActivHumTyp] where [Id_ActvCateg2] ='" & Me.ComboBoxCodeCatg.Text & "'"
            End If

            db.Connecter(databaseName)
            Me.ComboBoxNomTyp.Items.Clear()
            Me.ComboBoxCodeTyp.Items.Clear()
            Me.ComboBoxCodeTyp.Items.Add("")
            Me.ComboBoxNomTyp.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxCodeTyp.Items.Add(db.rdr(0))
                Me.ComboBoxNomTyp.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxCodeTyp.SelectedIndex = 0
            Me.ComboBoxNomTyp.SelectedIndex = 0
            db.Disconnecter()

        Catch ex As Exception
            MsgBox("Problème de connexion ")
        End Try
    End Sub
    Private Sub ComboBoxNomCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.TextChanged

        If Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomCatg.Text.Length
            Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
            Me.ComboBoxNomCatg.SelectionStart = Pos
            Me.ComboBoxNomCatg.SelectionLength = Me.ComboBoxNomCatg.Text.Length - Pos
            Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
        Else
            Me.ComboBoxCodeCatg.Text = ""
        End If
        initialisation()
        majTyp()

    End Sub
    Private Sub initialisation()
        Me.TextBoxDecoupageHab.Text = "Découpage de la typologie d'habitat"
        LabelNiv.Text = LibelleNiv(0)
        Niveau = 1
        Entrer = ""
        Dim i As Integer = 0
        While i < NbrNiv
            CodeNiv(i) = ""
            IdNiv(i) = ""
            i = i + 1
        End While
    End Sub
    Private Sub ComboBoxNomTyp_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomTyp.TextChanged

        If Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomTyp.Text.Length
            Me.ComboBoxNomTyp.SelectedIndex = Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text)
            Me.ComboBoxNomTyp.SelectionStart = Pos
            Me.ComboBoxNomTyp.SelectionLength = Me.ComboBoxNomTyp.Text.Length - Pos
            Me.ComboBoxCodeTyp.SelectedIndex = Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text)
        Else
            Me.ComboBoxCodeTyp.Text = ""
        End If
    End Sub


    Private Sub majAut()
        Try
            Entrer = ""
            'If Not idAct = "" Then
            '    db.myReq = "Select * FROM  [ "  & databaseName & "].[dbo].[ConcernTyp] where [Id_Activ]='" & idAct & "'"
            'Else
            db.myReq = "Select * FROM  [dbo].[ConcernTyp]"
            'End If

            db.Connecter(databaseName)
            Me.ComboBoxCodeAct.Items.Clear()
            Me.ComboBoxNomAct.Items.Clear()
            Me.ComboBoxCodeAct.Items.Add("")
            Me.ComboBoxNomAct.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxCodeAct.Items.Add(db.rdr(0))
                Me.ComboBoxNomAct.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxCodeAct.SelectedIndex = 0
            Me.ComboBoxNomAct.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomAct_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomAct.TextChanged

        If Me.ComboBoxNomAct.FindString(Me.ComboBoxNomAct.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomAct.Text.Length
            Me.ComboBoxNomAct.SelectedIndex = Me.ComboBoxNomAct.FindString(Me.ComboBoxNomAct.Text)
            Me.ComboBoxNomAct.SelectionStart = Pos
            Me.ComboBoxNomAct.SelectionLength = Me.ComboBoxNomAct.Text.Length - Pos
            Me.ComboBoxCodeAct.SelectedIndex = Me.ComboBoxNomAct.FindString(Me.ComboBoxNomAct.Text)
        Else
            Me.ComboBoxCodeAct.Text = ""
        End If

    End Sub


    Private Sub majCatgEsp()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[EspTypols]"
            db.Connecter(databaseName)
            Me.ComboBoxIdEspace.Items.Clear()
            Me.ComboBoxNomCatgEsp.Items.Clear()
            Me.ComboBoxIdEspace.Items.Add("")
            Me.ComboBoxNomCatgEsp.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdEspace.Items.Add(db.rdr(0))
                Me.ComboBoxNomCatgEsp.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdEspace.SelectedIndex = 0
            Me.ComboBoxNomCatgEsp.SelectedIndex = 0
            db.Disconnecter()
            majTypEspX()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomCatgEsp_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatgEsp.TextChanged

        If Me.ComboBoxNomCatgEsp.FindString(Me.ComboBoxNomCatgEsp.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomCatgEsp.Text.Length
            Me.ComboBoxNomCatgEsp.SelectedIndex = Me.ComboBoxNomCatgEsp.FindString(Me.ComboBoxNomCatgEsp.Text)
            Me.ComboBoxNomCatgEsp.SelectionStart = Pos
            Me.ComboBoxNomCatgEsp.SelectionLength = Me.ComboBoxNomCatgEsp.Text.Length - Pos
            Me.ComboBoxIdEspace.SelectedIndex = Me.ComboBoxNomCatgEsp.FindString(Me.ComboBoxNomCatgEsp.Text)
            Niveau = 1
            majTypEspX()
        Else
            Me.ComboBoxIdEspace.Text = ""
        End If
    End Sub


    Private Sub majTypEspX()

        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[EspaceTyp] WHERE " & Nomtable(4 + NbrNiv) & " = '" & Niveau & "' and  " & Nomtable(1) & "='" & Me.ComboBoxIdEspace.SelectedItem & "'"

        While niv < Niveau
            requeteX = requeteX & "and " & Nomtable(3 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While


        If Niveau = 1 Then
            Me.ButtonBack.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)

        Me.ComboBoxIdTypEspX.Items.Clear()
        Me.ComboBoxNomTypoX.Items.Clear()
        While db.rdr.Read()
            Me.ComboBoxNomTypoX.Items.Add(db.rdr(NbrNiv + 5))
            Me.ComboBoxIdTypEspX.Items.Add(db.rdr(0))
        End While
        db.Disconnecter()

        If ComboBoxIdTypEspX.Items.Count < 0 Then
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonNext.Enabled = True
        End If
    End Sub
    Private Sub ComboBoxNomTypoX_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomTypoX.TextChanged

        'If Me.ComboBoxNomTypoX.FindString(Me.ComboBoxNomTypoX.Text) > 0 Then
        Dim Pos As Int32 = Me.ComboBoxNomTypoX.Text.Length
        Me.ComboBoxNomTypoX.SelectedIndex = Me.ComboBoxNomTypoX.FindString(Me.ComboBoxNomTypoX.Text)
        Me.ComboBoxNomTypoX.SelectionStart = Pos
        Me.ComboBoxNomTypoX.SelectionLength = Me.ComboBoxNomTypoX.Text.Length - Pos
        Me.ComboBoxIdTypEspX.SelectedIndex = Me.ComboBoxNomTypoX.FindString(Me.ComboBoxNomTypoX.Text)

        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        NomNiv(Niveau - 1) = Me.ComboBoxNomTypoX.Text
        While niv < NbrNiv
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While

        IdNiv(Niveau - 1) = Me.ComboBoxIdTypEspX.Text
        'Else
        ' Me.ComboBoxIdTypEspX.Text = ""
        ' End If
    End Sub


    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""
        Niveau = Niveau + 1
        Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[EspaceTyp] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxIdTypEspX.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(2 + Niveau)
        End While

        majTypEspX()
    End Sub
    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        majTypEspX()
        Me.ComboBoxNomTypoX.SelectedIndex = Me.ComboBoxNomTypoX.Items.IndexOf(IdNiv(Niveau - 1))
    End Sub


    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxExp.Text = ""
        Me.TextBoxNote.Text = ""
        Me.TextBoxLocalistation.Text = ""
        initialisation()
    End Sub
    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""
            Dim INOut As String = ""
            If RadioButton1.Checked Then
                INOut = "true"
            ElseIf RadioButton2.Checked Then
                INOut = "False"
            End If
            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[ACTIVHUMSIT]([ID_Sit],[Id_EspTyp],[Id_TypActivHum],[InOut_Activ],[Loc_Activ],[DescImp_Activ],[ExpImp_Activ],[Note_Activ],[SIGID]) VALUES('" & db.idSite & "','" & Me.ComboBoxIdEspace.Text & "','" & Me.ComboBoxCodeTyp.Text & "','" & INOut & "','" & Me.TextBoxLocalistation.Text & "','" & Me.TextBoxSuperf.Text & "','" & Me.TextBoxExp.Text & "','" & Me.TextBoxNote.Text & "','" & Me.oidValue.Text & "')"

            Else
                db.myReq = "UPDATE  [dbo].[ACTIVHUMSIT] SET [ID_Sit] = '" & db.idSite & "',[Id_EspTyp] = '" & Me.ComboBoxIdEspace.Text & "',[Id_TypActivHum] = '" & Me.ComboBoxCodeTyp.Text & "',[InOut_Activ] = '" & INOut & "',[Loc_Activ] = '" & Me.TextBoxLocalistation.Text & "',[DescImp_Activ] = '" & Me.TextBoxSuperf.Text & "',[ExpImp_Activ] = '" & Me.TextBoxExp.Text & "',[Note_Activ] = '" & Me.TextBoxNote.Text & "',[SIGID]='" & Me.oidValue.Text & "' WHERE [Id_Activ]= '" & Me.DataGridViewTypol.SelectedCells(0).Value & "'"
            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        miseajour()

    End Sub

    Private Sub miseajour()
        Try
            db.myReq = "SELECT * FROM  [dbo].[ActivHumSit]"
            db.Connecter(databaseName)
            DataGridViewTypol.Rows.Clear()
            While db.rdr.Read()
                DataGridViewTypol.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6), db.rdr(7), db.rdr(8), db.rdr(9))
                idAct = db.rdr(0)
            End While
            db.Disconnecter()

            If DataGridViewTypol.RowCount - 1 = 0 Then
                Me.TextBoxLocalistation.Text = ""
                Me.TextBoxNote.Text = ""
                Me.TextBoxExp.Text = ""
            Else
                DataGridViewTypol.Rows(0).Selected = True
                Me.TextBoxLocalistation.Text = DataGridViewTypol.SelectedCells(5).Value
                Me.TextBoxNote.Text = DataGridViewTypol.SelectedCells(8).Value
                Me.TextBoxExp.Text = DataGridViewTypol.SelectedCells(7).Value
                Me.TextBoxSuperf.Text = DataGridViewTypol.SelectedCells(6).Value
                If InStr(1, DataGridViewTypol.SelectedCells(4).Value, "true", 1) = 0 Then
                    Me.RadioButton1.Checked = False
                    Me.RadioButton2.Checked = True
                Else
                    Me.RadioButton1.Checked = True
                    Me.RadioButton2.Checked = False
                End If

            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub



    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        Dim intBenif As New ActBenefice
        intBenif.idActiv = Me.DataGridViewTypol.SelectedCells(0).Value
        intBenif.Visible = True
    End Sub


    Private Sub DataGridViewTypol_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewTypol.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewTypol_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewTypol.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub


    Private Sub selection()
        Try
            If Not DataGridViewTypol.SelectedCells(0).ToString = "" Then
                Me.TextBoxLocalistation.Text = DataGridViewTypol.SelectedCells(5).Value
                Me.TextBoxNote.Text = DataGridViewTypol.SelectedCells(8).Value
                Me.TextBoxExp.Text = DataGridViewTypol.SelectedCells(7).Value
                Me.TextBoxSuperf.Text = DataGridViewTypol.SelectedCells(6).Value
                Me.oidValue.Text = DataGridViewTypol.SelectedCells(9).Value
                If InStr(1, DataGridViewTypol.SelectedCells(4).Value, "true", 1) = 0 Then
                    Me.RadioButton1.Checked = False
                    Me.RadioButton2.Checked = True
                Else
                    Me.RadioButton1.Checked = True
                    Me.RadioButton2.Checked = False
                End If
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub


    Private Sub ButtonDysf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDysf.Click
        Dim intImportDysf As New importerDysAct
        intImportDysf.idAct = Me.DataGridViewTypol.SelectedCells(0).Value
        intImportDysf.Visible = True
    End Sub



    ''' <summary>
    ''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private _workspace As Object
    Private _layerName As String
    Private geoForm As GeoLocation = Nothing
    Property layerName() As String
        Get
            Return _layerName
        End Get
        Set(ByVal value As String)
            _layerName = value
        End Set
    End Property

    Property workspace() As Object
        Get
            Return _workspace
        End Get
        Set(ByVal value As Object)
            _workspace = value
        End Set
    End Property

    Private Sub btnGetGeo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetGeo.Click
        If geoForm IsNot Nothing AndAlso geoForm.Visible Then
            Return
        End If

        geoForm = New GeoLocation()
        geoForm.Show()
        geoForm.workspace = Me.workspace
        geoForm.oidParentText = oidValue

    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[ACTIVHUMSIT] WHERE [Id_Activ]= '" & Me.DataGridViewTypol.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            db.Disconnecter()
            miseajour()
            If DataGridViewTypol.RowCount - 1 = 0 Then
                Return
            End If
            Dim listOid As List(Of String) = New List(Of String)

            listOid.Add(DataGridViewTypol.SelectedCells(9).Value)

            _workspace.deleteNodes(_layerName, listOid)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
   
End Class